<?php
function pr($array)
{
	echo "<pre>";
	print_r($array);
	echo "</pre>";
}

function randMD5()
{
	return md5(time().mt_rand(-999999999, 999999999));
}

function MYSQL_NOW()
{
	return date('Y-m-d H:i:s');
}

function Xregenerate_url($ignore = '', $onlyQS = false)
{
	$QUERY_STRING = '';

	if (!empty($_SERVER['QUERY_STRING']))
	{
		$QUERY_STRING = $_SERVER['QUERY_STRING'];
	}
	
	parse_str($QUERY_STRING, $global_param_list);

	// Transform ignore param into an array:
	if (empty($ignore))
	{
		$ignore = array();
	}
	elseif (!is_array($ignore))
	{
		$ignore = explode(',', $ignore);
	}

	//building query_string
	$query_string = array();
	if (!empty($global_param_list))
	{
		foreach ($global_param_list as $k => $v)
		{
			//remove ignored params
			if (in_array($k, $ignore))
			{
				continue;
			}
			
			$query_string[$k] = sprintf('%s=%s', $k, $v);
		}
	}

//		return $_SERVER['PHP_SELF'].$url;
	$script_filename = $_SERVER['REQUEST_URI'];
	if (strpos($script_filename, '?') !== false)
	{
		$script_filename = substr($script_filename, 0, strpos($script_filename, '?'));
	}

	$query_string = implode('&', $query_string);
	if (!empty($query_string))
	{
		$query_string = "?".$query_string;
	}
	
	if ($onlyQS)
	{
		return $query_string;
	}

	return $script_filename.$query_string;
}

function Xurl_add($url, $url_param, $url_value )
{
	if (strpos($url, '?') === false)
	{
		$url .= "?".$url_param."=".$url_value;
	}
	else
	{
		$url .= "&".$url_param."=".$url_value;
	}
	return $url;
}

function REQUEST_URI()
{
	return (isset($_SERVER['REQUEST_URI'])) ? rawurlencode($_SERVER['REQUEST_URI']) : ''; 
}

function date_modified($date)
{
	if ($date != '0000-00-00 00:00:00')
	{
		return date('d-m-Y H:i', strtotime($date));
	}
	else
	{
		return 'Не изменялся';
	}
}

/*
 * Password generation
 * Lenght must be a multiple of 2 !! So 14 will work, 15 won't, 16 will, 17 won't and so on
 * */
function humanPassword($length)
{
    $conso = array("b","c","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","x","y","z");
    $vocal = array("a","e","i","o","u");
    $password = "";
    srand ((double)microtime()*1000000);
    $max = $length/2;
    
    for($i = 1; $i <= $max; $i++)
    {
	    $password .= $conso[rand(0,19)];
	    $password .= $vocal[rand(0,4)];
    }
    
    return $password;
}
 
function selectedOptions($all_options = array(), $selected_options = array())
{
	if (!empty($selected_options))
	{
		$arr = array();
		foreach($selected_options as $id)
		{
			$arr[$id] = $all_options[$id];
		}	
		$selected_options = $arr;
	}
	
	return $selected_options;
}

function sendEmail($data)
{
	$CI =& get_instance();
	$CI->load->library('email');		
	$CI->load->model(array('/public/email_model'));		
	
    $data['site_name'] = $CI->config->item('site_name');
    $data['site_url'] = $CI->config->item('site_url') . $CI->config->item('base_url');
    $dif = array();

    $k=0;
    
    foreach($data as $key =>$value){
        $placeholders_data[$k] = $value;
        $dif[$k] = '{'.$key.'}';
        $k++;
    }

	$email = $CI->email_model->get($data['email_name']);

	$subject = $email['subject'];
	$subject = str_replace($dif, $placeholders_data, $subject);
	
    $message = htmlspecialchars_decode($email['message']);
    $message = str_replace($dif, $placeholders_data, $message);
    //pr($message);
    //die;
/*
    $config['mailtype'] = 'html';
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'mail.l7hosting.in';
    $config['smtp_user'] = 'relay@l7hosting.in';
    $config['smtp_pass'] = '%relay2011';
    $config['smtp_port'] = '2525';

    $config['charset'] = 'utf-8';
	$config['newline'] = '\r\n';
*/

    $config = $CI->config->item('mailConfig');
    $CI->email->initialize($config);
	$CI->email->set_newline("\r\n"); 
	$CI->email->from($data['toemail'], EMAIL_FROM_NAME);
	$CI->email->to($data['email']);
	//$CI->email->to($CI->config->item('support_emails'));					
	$CI->email->subject($subject);
	$CI->email->message($message);
	//echo $message;
	$CI->email->send();

}

function generatecode(){
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;

    while ($i <= 16) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }

    return $pass;

}

function generateNumber(){
    $chars = '123456789123456789123456789123456789123456789';
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;

    while ($i <= 7) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }

    return $pass;
}
function array_differ($array1, $array2){
    $result=array();
     foreach($array1 as $key => $value) {
            if($array2[$key]!==$value){
                $result[$key]=$value;
            }
        }
    foreach($array2 as $key => $value) {
            if($array1[$key]!==$value){
                $result[$key]=$value;
            }
        }
        
    return $result;
}
?>